package com.hoshino.momochi.controller;

import com.hoshino.momochi.model.DTO.BlogDTO;
import com.hoshino.momochi.model.domain.BlogLike;
import com.hoshino.momochi.service.BlogLikeService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import com.hoshino.momochi.util.PageCheck;
import com.hoshino.momochi.util.SomeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
public class BlogLikeController {

    @Autowired
    private BlogLikeService blogLikeService;

    private final String USER_ID = CodeEnum.USERID.getMessage();

    /**
     * 点赞
     *
     * @param request
     * @param blogDTO 需要字段 blog_id
     * @return
     */
    @RequestMapping("api/blogLike/add")
    public JsonResponse addBlogLike(HttpServletRequest request, @RequestBody BlogDTO blogDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long bid = blogDTO.getBlog_id();
        BlogLike ad = new BlogLike();
        ad.setUserId(uid);
        ad.setBlogId(bid);
        BlogLike blogLike = null;
        if (Objects.isNull(bid) || Objects.nonNull(blogLike = blogLikeService.findBlogLikeByTwoId(ad))) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        if (blogLike == null) {
            // 没点过赞
            ad.setCreateTime(new Date());
            if (bid != null) {
                blogLikeService.addBlogLike(ad);
                return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
            }
        }

        return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
    }

    /**
     * 取消点赞
     *
     * @param request
     * @param blogDTO
     * @return
     */
    @RequestMapping("api/blogLike/del")
    public JsonResponse delBlogLike(HttpServletRequest request, @RequestBody BlogDTO blogDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long bid = blogDTO.getBlog_id();
        BlogLike blogLike = new BlogLike();
        blogLike.setBlogId(bid);
        blogLike.setUserId(uid);
        blogLikeService.delBlogLike(blogLike);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 点赞计数
     *
     * @param request
     * @param blogDTO 需求字段 blog_id
     * @return
     */
    @RequestMapping("api/blogLike/count")
    public JsonResponse countBlogLike(HttpServletRequest request, @RequestBody BlogDTO blogDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long bid = blogDTO.getBlog_id();
        if (Objects.isNull(bid)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        BlogLike blogLike = new BlogLike();
        blogLike.setBlogId(bid);
        blogLike.setUserId(uid);
        Integer count = blogLikeService.getCountFromBlog(blogLike);
        if (Objects.isNull(count)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        return JsonResponse.success(count, CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取当前登录用户的点赞记录列表
     *
     * @param request need_page page_size(null)
     * @return
     */
    @RequestMapping("api/blogLike/getUserLikes")
    public JsonResponse getUserLikes(HttpServletRequest request, @RequestBody BlogDTO blogDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        BlogLike blogLike = new BlogLike();
        blogLike.setUserId(uid);
        List<BlogLike> likeList = blogLikeService.getBlogLikeList(blogLike);
        return JsonResponse.success(PageCheck.pageCheck(SomeUtils.sortByCreateTime(likeList).stream().map(s -> s.getBlog()).toList(), blogDTO.getNeed_page(), blogDTO.getPage_size()), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 检测用户是否点过赞
     *
     * @param request
     * @param blogDTO 传入字段 blog_id
     * @return
     */
    @RequestMapping("api/blogLike/checkUserLike")
    public JsonResponse checkUserLike(HttpServletRequest request, @RequestBody BlogDTO blogDTO) {
        Long bid = blogDTO.getBlog_id();
        Long uid = (Long) request.getAttribute(USER_ID);
        if (Objects.isNull(blogLikeService.checkUserLikeBlog(uid, bid))) {
            return JsonResponse.success(CodeEnum.LIKENEXIST.getCode(), CodeEnum.LIKENEXIST.getMessage());
        }
        return JsonResponse.success(CodeEnum.LIKEEXIST.getCode(), CodeEnum.LIKEEXIST.getMessage());
    }

    /**
     * 获取 谁赞了我列表， 包含 博客简要信息 ，点赞人信息
     *
     * @param request
     * @return
     */
    @RequestMapping("api/blogLike/getWhoLikeMe")
    public JsonResponse getWhoLikeMe(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<BlogLike> bl = blogLikeService.getWhoLikeMe(uid);
        return JsonResponse.success(PageCheck.makeData(SomeUtils.sortByCreateTime(bl)), CodeEnum.SUCCESS.getMessage());
    }
}
